package com.wxmit.starter.file.config;

import cn.hutool.core.collection.CollectionUtil;
import com.wxmit.base.util.StringUtils;
import com.wxmit.starter.file.FileApi;
import com.wxmit.starter.file.core.SystemFileApiImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author huangxiong
 * @date 2024/03/20 09:51
 */
@Configuration
public class SystemFileAutoConfiguration {


    @Bean
    public FileApi getSystemFileApi(
            @Value("${ss.file.fileRealm:}") String fileRealm,
            @Value("${ss.file.prefix:}") String prefix,
            @Value("${ss.file.urlPrefix:}") String urlPrefix ,
            @Value("${ss.file.maxSize:0}") Long maxSize,
            @Value("${ss.file.fileNameLength:0}") Integer fileNameLength,
            @Value("${ss.file.allowedExtension:}") List<String> allowedExtension
            ) {

        initShushanFileConfig(fileRealm,prefix,urlPrefix,maxSize,fileNameLength,allowedExtension);

        return new SystemFileApiImpl();
    }

    /**
     *  初始化 文件 配置信息
     * @param fileRealm 文件域名前缀
     * @param prefix 文件磁盘路径前缀
     * @param maxSize 最大地址
     * @param fileNameLength 文件名最大长度
     * @param allowedExtension 允许上传的扩展名
     */
    private void initShushanFileConfig( String fileRealm, String prefix, String urlPrefix,  Long maxSize,Integer fileNameLength, List<String> allowedExtension){
        ShushanFileConfig.fileRealm = StringUtils.isEmpty(fileRealm) ? ShushanFileConfig.fileRealm : fileRealm;
        ShushanFileConfig.baseDir = StringUtils.isEmpty(prefix) ? ShushanFileConfig.baseDir : prefix;
        ShushanFileConfig.urlPrefix = StringUtils.isEmpty(urlPrefix) ? ShushanFileConfig.urlPrefix : urlPrefix;

        ShushanFileConfig.fileMaxSize = Objects.isNull(maxSize) || maxSize.intValue() == 0  ? ShushanFileConfig.fileMaxSize : maxSize;
        ShushanFileConfig.fileNameMaxLength = Objects.isNull(fileNameLength) || fileNameLength == 0 ? ShushanFileConfig.fileNameMaxLength : fileNameLength ;
        ShushanFileConfig.allowedExtension = CollectionUtil.isEmpty(allowedExtension)? ShushanFileConfig.allowedExtension : allowedExtension;

    }

}
